package com.yjt.sousou.integral;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.utils.GetTimeUtil;
import com.yjt.sousou.widget.HollowPieChart;
import com.yjt.sousou.widget.ScaleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：83734
 * 时间：2018/11/30
 * 描述：我的积分
 */
public class IntegralActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_last_month)
    TextView mTvLastMonth;
    @BindView(R.id.tv_current_time)
    TextView mTvCurrentTime;
    @BindView(R.id.tv_next_month)
    TextView mTvNextMonth;
    @BindView(R.id.scaleview)
    ScaleView mScaleview;
    @BindView(R.id.pie_chart)
    HollowPieChart mPieChart;

    private String selectDate = "";
    private String currentTime;
    private int currentYear;
    private int currentMonth;
    @Override
    protected Object setContentLayout() {
        return R.layout.activity_integral;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        mTvTitle.setText(R.string.my_integral);
        currentTime = GetTimeUtil.getYMTime(System.currentTimeMillis());
        currentYear = Integer.valueOf(currentTime.substring(0,4));
        currentMonth = Integer.valueOf(currentTime.substring(5));
        selectDate = currentTime;
        mTvCurrentTime.setText(currentTime);
        double[] scales = new double[2];
        scales[0] = 0.5;
        scales[1] = 0.5;
        mScaleview.setScales(scales);

        int[] mColors = {ContextCompat.getColor(this,R.color.circle_integral_common),
                ContextCompat.getColor(this,R.color.circle_integral_well),
                ContextCompat.getColor(this,R.color.circle_integral_excellent)};
        List<PieDataEntity> dataEntities = new ArrayList<>();
        for(int i = 0;i<3;i++){
            PieDataEntity entity = new PieDataEntity("name"+i,i+1,mColors[i]);
            dataEntities.add(entity);
        }
        mPieChart.setDataList(dataEntities);
    }

    @OnClick({R.id.tv_last_month,R.id.tv_next_month,R.id.tv_to_ranking})
    void onClick(View view){
        switch (view.getId()){
            case R.id.tv_last_month:
                mTvNextMonth.setTextColor(ContextCompat.getColor(this,R.color.white));
                mTvNextMonth.setClickable(true);
                selectDate = GetTimeUtil.getLastMonth(selectDate, -1);
                mTvCurrentTime.setText(selectDate);
                break;
            case R.id.tv_next_month:
                if(!TextUtils.isEmpty(selectDate)){
                    int selectMonth = Integer.valueOf(selectDate.substring(5));
                    int selectYear = Integer.valueOf(selectDate.substring(0,4));
                    if(selectYear == currentYear){
                        if(selectMonth + 1 == currentMonth){
                            mTvNextMonth.setTextColor(ContextCompat.getColor(this,R.color.gray_text));
                            mTvNextMonth.setClickable(false);
                        }
                        if(!(selectMonth + 1 > currentMonth)){
                            selectDate = GetTimeUtil.getLastMonth(selectDate,1);
                            mTvCurrentTime.setText(selectDate);
//                            fetchData();
                        }
                    }else if(selectYear < currentYear){
                        selectDate = GetTimeUtil.getLastMonth(selectDate,1);
                        mTvCurrentTime.setText(selectDate);
//                        fetchData();
                    }

                }
                break;
            case R.id.tv_to_ranking:
                startActivity(RankingActivity.class);
                break;
        }
    }
}
